/*
 *    Copyright © OpenAtom Foundation.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.inspur.edp.bff.core.manager.builtinimpl;

import com.inspur.edp.bff.api.exception.BffFrameworkException;
import com.inspur.edp.bff.api.exception.ErrorCodes;
import com.inspur.edp.bff.api.manager.IFSManagerContext;
import com.inspur.edp.bff.core.manager.FSManager;
import com.inspur.edp.bff.spi.AbstractBffEntityDeserConvertor;
import com.inspur.edp.bff.spi.AbstractBffEntitySerConvertor;
import com.inspur.edp.bff.spi.BffChangeDeserConvertor;
import com.inspur.edp.bff.spi.BffChangeSerConvertor;
import com.inspur.edp.bff.spi.BffEntityDeserConvertor;
import com.inspur.edp.bff.spi.BffEntitySerConvertor;
import com.inspur.edp.bff.spi.VMHelpConfig;
import com.inspur.edp.bff.spi.changeset.AbstractBffChangeJsonDeserializer;
import com.inspur.edp.bff.spi.changeset.AbstractBffChangeJsonSerializer;
import com.inspur.edp.cef.spi.entity.resourceInfo.ModelResInfo;
import com.inspur.edp.cef.spi.entity.resourceInfo.builinImpls.CefEntityResInfoImpl;
import com.inspur.edp.cef.spi.entity.resourceInfo.builinImpls.CefModelResInfoImpl;

public abstract class BffBuiltInManager extends FSManager {
//    @Override
//    protected final ModelResInfo innerGetModelInfo() {
//        if(getManagerCacheInfo().getModelResInfo()!=null)
//            return getManagerCacheInfo().getModelResInfo();
//        ModelResInfo modelResInfo=getModelInfo();
//        getManagerCacheInfo().setModelResInfo(modelResInfo);
//        return modelResInfo;
//    }

    @Override
    public final ModelResInfo getModelInfo() {
        return getCefModelResourceInfo();
    }

    public abstract CefModelResInfoImpl getCefModelResourceInfo();

    protected IFSManagerContext getContext() {
        return getBffContext();
    }

    protected final void processResponse() {
        getContext().processResponse();
    }

    @Override
    protected String getHelpId(String codeName, String labelId) {
        VMHelpConfig config = getHelpConfig(codeName, labelId);
        if (config == null) {
            throw new BffFrameworkException(ErrorCodes.BFF_FRAMEWORK_1039, labelId);
        }
        return config.getHelpId();
    }

    protected void addExtends() {
        String classFullName = "com.inspur.edp.customize.bff.BffCtmExtendProvider";
        try {
            Class providerClass = Class.forName(classFullName);
            addExtClass(providerClass);
        } catch (ClassNotFoundException e) {
            throw new BffFrameworkException(e, ErrorCodes.BFF_FRAMEWORK_1031);
        }
    }


    @Override
    protected AbstractBffEntityDeserConvertor getDataJsonDeSerizlize() {
        return new BffEntityDeserConvertor(getCefModelResourceInfo().getRootNodeCode(), true, getType(),
                (CefEntityResInfoImpl) getCefModelResourceInfo().getCustomResource(getCefModelResourceInfo().getRootNodeCode()));
    }

    @Override
    protected AbstractBffEntitySerConvertor getDataJsonSerizlize() {
        return new BffEntitySerConvertor(getCefModelResourceInfo().getRootNodeCode(), true, getType(),
                (CefEntityResInfoImpl) getCefModelResourceInfo().getCustomResource(getCefModelResourceInfo().getRootNodeCode()));
    }

    @Override
    protected AbstractBffChangeJsonSerializer getChangeSerializer() {
        return new BffChangeSerConvertor(getCefModelResourceInfo().getRootNodeCode(), true, getType(),
                (CefEntityResInfoImpl) getCefModelResourceInfo().getCustomResource(getCefModelResourceInfo().getRootNodeCode()));
    }

    @Override
    protected AbstractBffChangeJsonDeserializer getChangeDeserializer() {
        return new BffChangeDeserConvertor(getCefModelResourceInfo().getRootNodeCode(), true, getType(),
                (CefEntityResInfoImpl) getCefModelResourceInfo().getCustomResource(getCefModelResourceInfo().getRootNodeCode()));
    }

    private String getType() {
        CefEntityResInfoImpl cefEntityResInfo = (CefEntityResInfoImpl) getCefModelResourceInfo().getCustomResource(getCefModelResourceInfo().getRootNodeCode());
        String configId = cefEntityResInfo.getConfigId();
        if (configId.isEmpty()) {
            throw new BffFrameworkException(ErrorCodes.BFF_FRAMEWORK_1040, getCefModelResourceInfo().getRootNodeCode());
        }
        if (!configId.startsWith("com.") && configId.contains(".")) {
            configId = "com." + configId;
        }
        return configId;
    }
}
